#!/bin/bash

# Interactive Commit Script - 交互式提交脚本
# 类似于 commitizen 的功能，帮助用户规范化提交信息

set -e

# 设置终端编码为UTF-8，确保中文输入正常
export LANG=zh_CN.UTF-8
export LC_ALL=zh_CN.UTF-8

# 启用readline编辑功能（如果在bash中）
if [[ $BASH_VERSION ]]; then
    set -o emacs  # 启用emacs模式的行编辑
fi

# 颜色定义
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
PURPLE='\033[0;35m'
CYAN='\033[0;36m'
WHITE='\033[1;37m'
NC='\033[0m' # No Color

# 清屏并显示标题
clear
echo -e "${CYAN}🚀 Frontend Toolkit - 交互式提交工具${NC}"
echo -e "${CYAN}═══════════════════════════════════════${NC}"
echo ""

# 检查是否有待提交的更改
if [ -z "$(git diff --cached --name-only)" ]; then
    echo -e "${YELLOW}⚠️  没有找到已暂存的文件。${NC}"
    echo -e "${WHITE}请先使用 ${GREEN}git add${WHITE} 命令添加要提交的文件。${NC}"
    echo ""
    echo -e "${BLUE}未暂存的文件:${NC}"
    git status --porcelain | grep -E "^( M| D| ??)" || echo "  无"
    echo ""
    exit 1
fi

# 显示待提交的文件
echo -e "${GREEN}📁 待提交的文件:${NC}"
git diff --cached --name-only | sed 's/^/  ✓ /'
echo ""

# 提交类型选择
echo -e "${BLUE}📝 请选择提交类型:${NC}"
echo ""
echo -e "  ${GREEN}1)${NC} ${WHITE}feat${NC}     - ✨ 新功能 (A new feature)"
echo -e "  ${GREEN}2)${NC} ${WHITE}fix${NC}      - 🐛 问题修复 (A bug fix)"
echo -e "  ${GREEN}3)${NC} ${WHITE}docs${NC}     - 📚 文档更新 (Documentation only changes)"
echo -e "  ${GREEN}4)${NC} ${WHITE}style${NC}    - 🎨 代码格式 (Changes that do not affect the meaning of the code)"
echo -e "  ${GREEN}5)${NC} ${WHITE}refactor${NC} - ♻️  重构 (A code change that neither fixes a bug nor adds a feature)"
echo -e "  ${GREEN}6)${NC} ${WHITE}perf${NC}     - ⚡ 性能优化 (A code change that improves performance)"
echo -e "  ${GREEN}7)${NC} ${WHITE}test${NC}     - 🧪 增加测试 (Adding missing tests or correcting existing tests)"
echo -e "  ${GREEN}8)${NC} ${WHITE}chore${NC}    - ⚙️  构建过程或辅助工具的变动 (Changes to the build process or auxiliary tools)"
echo -e "  ${GREEN}9)${NC} ${WHITE}ci${NC}       - 🔧 CI配置 (Changes to our CI configuration files and scripts)"
echo -e "  ${GREEN}10)${NC} ${WHITE}build${NC}   - 📦 构建系统 (Changes that affect the build system or external dependencies)"
echo -e "  ${GREEN}11)${NC} ${WHITE}revert${NC}  - ◀️  回滚 (Reverts a previous commit)"
echo ""

# 读取用户选择
while true; do
    if [[ $BASH_VERSION ]]; then
        read -e -p "$(echo -e ${YELLOW}请输入选择 [1-11]: ${NC})" choice
    else
        read -p "$(echo -e ${YELLOW}请输入选择 [1-11]: ${NC})" choice
    fi
    case $choice in
        1) commit_type="feat"; break ;;
        2) commit_type="fix"; break ;;
        3) commit_type="docs"; break ;;
        4) commit_type="style"; break ;;
        5) commit_type="refactor"; break ;;
        6) commit_type="perf"; break ;;
        7) commit_type="test"; break ;;
        8) commit_type="chore"; break ;;
        9) commit_type="ci"; break ;;
        10) commit_type="build"; break ;;
        11) commit_type="revert"; break ;;
        *) echo -e "${RED}❌ 无效选择，请输入 1-11 之间的数字${NC}" ;;
    esac
done

echo ""
echo -e "${GREEN}✓ 已选择提交类型: ${WHITE}$commit_type${NC}"
echo ""

# 询问作用域 (可选)
echo -e "${BLUE}🎯 作用域 (可选，按回车跳过):${NC}"
echo -e "${WHITE}例如: auth, ui, api, docs, config 等${NC}"
if [[ $BASH_VERSION ]]; then
    read -e -p "$(echo -e ${YELLOW}请输入作用域: ${NC})" scope
else
    read -p "$(echo -e ${YELLOW}请输入作用域: ${NC})" scope
fi

# 构建作用域部分
if [ -n "$scope" ]; then
    scope_part="($scope)"
    echo -e "${GREEN}✓ 作用域: ${WHITE}$scope${NC}"
else
    scope_part=""
    echo -e "${GREEN}✓ 跳过作用域${NC}"
fi

echo ""

# 输入提交描述
echo -e "${BLUE}📄 提交描述:${NC}"
echo -e "${WHITE}请简洁明了地描述此次提交的内容${NC}"
while true; do
    if [[ $BASH_VERSION ]]; then
        read -e -p "$(echo -e ${YELLOW}请输入描述: ${NC})" description
    else
        read -p "$(echo -e ${YELLOW}请输入描述: ${NC})" description
    fi
    if [ -n "$description" ]; then
        break
    else
        echo -e "${RED}❌ 描述不能为空，请重新输入${NC}"
    fi
done

echo ""
echo -e "${GREEN}✓ 描述: ${WHITE}$description${NC}"
echo ""

# 询问是否添加详细说明 (可选)
echo -e "${BLUE}📋 详细说明 (可选):${NC}"
echo -e "${WHITE}如果需要添加更详细的说明，请输入。按回车跳过。${NC}"
if [[ $BASH_VERSION ]]; then
    read -e -p "$(echo -e ${YELLOW}详细说明: ${NC})" body
else
    read -p "$(echo -e ${YELLOW}详细说明: ${NC})" body
fi

# 构建完整的提交信息
commit_message="$commit_type$scope_part: $description"

if [ -n "$body" ]; then
    commit_message="$commit_message

$body"
fi

# 显示最终的提交信息
echo ""
echo -e "${PURPLE}═══════════════════════════════════════${NC}"
echo -e "${PURPLE}📋 最终提交信息:${NC}"
echo -e "${PURPLE}═══════════════════════════════════════${NC}"
echo -e "${WHITE}$commit_message${NC}"
echo -e "${PURPLE}═══════════════════════════════════════${NC}"
echo ""

# 确认提交
while true; do
    if [[ $BASH_VERSION ]]; then
        read -e -p "$(echo -e ${YELLOW}确认提交? [y/N]: ${NC})" confirm
    else
        read -p "$(echo -e ${YELLOW}确认提交? [y/N]: ${NC})" confirm
    fi
    case $confirm in
        [Yy]* ) break ;;
        [Nn]* | "" ) 
            echo -e "${RED}❌ 取消提交${NC}"
            exit 1 
            ;;
        * ) echo -e "${RED}请输入 y 或 n${NC}" ;;
    esac
done

echo ""
echo -e "${CYAN}🚀 正在提交...${NC}"
echo ""

# 执行提交
if git commit -m "$commit_message"; then
    echo ""
    echo -e "${GREEN}✅ 提交成功！${NC}"
    echo ""
    echo -e "${BLUE}📊 提交信息:${NC}"
    git log -1 --oneline
    echo ""
else
    echo ""
    echo -e "${RED}❌ 提交失败${NC}"
    exit 1
fi